package com.example.neteasecloudmusic.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.bean.SongDetailInfo;
import com.example.neteasecloudmusic.bean.playListBean.AlbumSongInfo;
import com.example.neteasecloudmusic.bean.playListBean.PlayList;
import com.example.neteasecloudmusic.bean.playListBean.Tracks;

import com.example.neteasecloudmusic.greenDaoGen.SongDetailController;
import com.example.neteasecloudmusic.help.DestroyActivityUtil;
import com.example.neteasecloudmusic.ui.base.BaseActivity;
import com.example.neteasecloudmusic.ui.song_list_detail.PlayContract;
import com.example.neteasecloudmusic.ui.song_list_detail.PlayListAdapter;
import com.example.neteasecloudmusic.ui.song_list_detail.PlayListPresenter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class PlayListActivity extends BaseActivity implements View.OnClickListener, PlayContract.IPlayListView, PlayListAdapter.OnItemClickListener {

    public static final String PLAYLIST_ID = "playlist_id";
    private Toolbar toolbar;
    private PlayListPresenter playListPresenter;
    private ImageView playListImageView, iv_play_all;
    private TextView playListTextView, tv_play_all;
    private PlayListAdapter playListAdapter;
    private ImageView imageView;
    private List<Tracks> tracks;
    private SongDetailController mSongDetailController;

    @SuppressLint("ResourceType")
    @Override
    protected void initViews() {

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        toolbar = findViewById(R.id.toolbar2);
        iv_play_all = findViewById(R.id.imageView2);
        tv_play_all = findViewById(R.id.textView2);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        toolbar.setNavigationIcon(R.drawable.ic_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        List<Tracks> tracks = new ArrayList<>();
        RecyclerView recyclerView = findViewById(R.id.recycleView_playlist);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        playListAdapter = new PlayListAdapter(recyclerView, this, tracks);

        playListAdapter.setOnItemClickListener(this);
        iv_play_all.setOnClickListener(this);
        tv_play_all.setOnClickListener(this);

        recyclerView.setAdapter(playListAdapter);

        playListImageView = findViewById(R.id.imageView);
        playListTextView = findViewById(R.id.textView);
        String id = getIntent().getStringExtra(PLAYLIST_ID);
        playListPresenter = new PlayListPresenter(this);
        playListPresenter.getPlayList(id);
        imageView = findViewById(R.id.imageView2);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_playlist;
    }



    @Override
    public void getPlayListSuccess(PlayList playList) {
        toolbar.setTitle(playList.getName());
        playListTextView.setText(playList.getDescription());
        Glide.with(this)
                .load(playList.getCoverImgUrl())
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(playListImageView);
        tracks = playList.getTracks();
        playListAdapter.setData(tracks);
    }

    @Override
    public void getPlayListFailure(Throwable throwable) {
        Toast.makeText(this, "playList false", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        onItemClick(0);
    }

    @Override
    public void onItemClick(int position) {
        DestroyActivityUtil.destroyActivity("PlayMusicActivity");
        Intent intent = new Intent(PlayListActivity.this, PlayMusicActivity.class);
        SongDetailInfo detailInfo = new SongDetailInfo();
        mSongDetailController = SongDetailController.getInstance(getApplicationContext());
       List<SongDetailInfo> songDetailInfos = mSongDetailController.searchAll();
       if (songDetailInfos == null || songDetailInfos.size() != tracks.size()) {
           for (int i = 0; i < tracks.size(); i++) {
               // 必须每次循环的时候new出新的SongDetailInfo对象, 否则内存地址冲突 汗!
                detailInfo = new SongDetailInfo();
                detailInfo.setId((long) i);
                detailInfo.setSongName(tracks.get(i).getName());
                detailInfo.setSingerName(tracks.get(i).getAr().get(0).getName());
                detailInfo.setUrl(tracks.get(i).getRurl());
                detailInfo.setPic(tracks.get(i).getAl().getPicUrl());
                mSongDetailController.insertOrReplace(detailInfo);
            }
        }
        intent.putExtra("position",position);
        startActivity(intent);
    }
}
